var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');



var app = getApp();

Page({
  data: {
    sts:['待审核','已退款','取消退款'],
    array: ['请选择退款原因', '多拍','质量问题','发错货','不想要了','协商退货', '其他'],
    sortlist:[
      { id: 0, title:"请选择退款原因"},
      { id: 1, title: "多拍" },
      { id: 2, title: "质量问题" },
      { id: 3, title: "发错货" },
      { id: 4, title: "不想要了" },
      { id: 5, title: "协商退货" },
      { id: 6, title: "其他" }
    ],
    index: 0,
    content:'',
    contentLength:0,
    mobile:'',
    imgs:[],
    userInfo:null,
    total:0,
    exist:false,
    log:null,
    id:0,
    orderId:''
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    });
  },
  mobileInput: function (e) {
    let that = this;
    this.setData({
      mobile: e.detail.value,
    });
  },
  upload: function () 
  {
    var that = this;
    var arr = that.data.arr;
    var info = that.data.info;
    
    util.upload().then(function (res) 
    {
      if (res.ST == 100) 
      { 
        var img_url = res.Url;
        var imgs=that.data.imgs;
        imgs.push(img_url);
        that.setData({imgs:imgs});
      }
      
    });
    
  },
  del_img:function(e){
    var im=e.currentTarget.dataset.im;
    var imgs=this.data.imgs;
    for(var i=0;i<imgs.length;i++)
    {
      if(im==i)
      {
        imgs.splice(i,1);
      }
    }
    this.setData({imgs:imgs});
  },
  contentInput: function (e) {
   
    let that = this;
    this.setData({
      contentLength: e.detail.cursor,
      content: e.detail.value,
    });
  },
  cleanMobile:function(){
    let that = this;

  },
  
  sbmitFeedback : function(e){
    let that = this;
    if (that.data.index == 0)
    {
      util.showErrorToast('请选择反馈类型');
      return false;
    }
    var id=that.data.id;
    var sort_id = that.data.sortlist[that.data.index].id;
    var sort_name = that.data.sortlist[that.data.index].title;
    if (that.data.content == '') {
      util.showErrorToast('请输入反馈内容');
      return false;
    }

    

    if (that.data.userInfo ==null) {
      util.showErrorToast('请先登录');
      return false;
    }
    var userid=that.data.userInfo.Id;
    var total=that.data.total;
    var files=that.data.imgs.join(';');
    //wx.showLoading({title: '提交中...',mask:true});
    var orderId=that.data.orderId;
    var pd = { RefundDesc: that.data.content, SortID: sort_id, OrderID: orderId, UserID: userid, OrderItemID:id,
      RefundFile:files,
      RefundPrice:total,
      ST:0
    };
    
    util.request(api.AddRefund,pd).then(function (res) {
      if (res.Status === 100) 
      {
        //wx.hideLoading();
        wx.showToast({
          title: '退款申请提交成功',
          icon: 'success',
          duration: 2000,
          complete: function () {
            that.setData({
              index: 0,
              content: '',
              contentLength: 0,
              mobile: ''
            });
            wx.navigateBack({delta: -2});
          }
        });
      } else {
        util.showErrorToast('提交失败');
      }
      
    });
  },
  get_refund2:function(){
    // 全退
    var that=this;
    var orderId=that.data.orderId;
    var pd = { OrderID:orderId};
    util.request(api.GetRefundInfo2,pd,'GET').then(function (res) {
      if (res.Status === 100) 
      { 
          that.setData({exist:true});
          that.setData({log:res.Data});
          that.setData({content:res.Data.RefundDesc});
          if(res.Data.RefundFile!="")
          {
            var arr=new Array();
            if(res.Data.RefundFile.indexOf(';')>-1)
            {
              arr=res.Data.RefundFile.split(';');
            }
            else
            {
              arr.push(res.Data.RefundFile);
            }
            that.setData({imgs:arr});
          }
          that.setData({index:res.Data.SortID});
      } 
      else 
      {
        that.setData({exist:false});
      }
      
    });

  },
  get_refund:function(){
    var that=this;
    var id=that.data.id;
    var pd = { ItemId:id};
    util.request(api.GetRefundInfo,pd,'GET').then(function (res) {
      if (res.Status === 100) 
      { 
          that.setData({exist:true});
          that.setData({log:res.Data});
          that.setData({content:res.Data.RefundDesc});
          if(res.Data.RefundFile!="")
          {
            var arr=new Array();
            if(res.Data.RefundFile!='')
            {
              if(res.Data.RefundFile.indexOf(';')>-1)
              {
                arr=res.Data.RefundFile.split(';');
              }
              else
              {
                arr.push(res.Data.RefundFile);
              }
            }
            that.setData({imgs:arr});
            
          }
          that.setData({index:res.Data.SortID});
      } 
      else 
      {
        that.setData({exist:false});
      }
      
    });
  },
  onLoad: function (options) 
  {
    if(options.id)
    {
      this.setData({id:options.id});
      this.get_info();
      this.get_refund();
    }
    if(options.orderId)
    {
      this.setData({orderId:options.orderId});      
      this.getOrderDetail();
      this.get_refund2();
    }
     var userInfo=wx.getStorageSync('userInfo');
    if (userInfo!="")
    {
      this.setData({ userInfo: userInfo});
    }
    else
    {
      wx.navigateTo({url: '/pages/auth/mobile/mobile'})
    }
    
    
    
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  },

  getOrderDetail() {
    let that = this;
    util.request(api.OrderDetail, {
      id: that.data.orderId
    },'GET').then(function (res) {
      if (res.Status === 100) 
      {
        res.Data.CreateDate = util.formatTime(res.Data.CreateDate);
        that.setData({orderInfo: res.Data});
        that.setData({total:res.Data.Total.toFixed(2)});
        //that.payTimer();
      }
    });
  },

  get_info:function(){
    var that=this;
    var id=that.data.id;
    if(id!=''&&id!=undefined)
    {
      var pd = { Id: id};
      util.request(api.GetOrderItem,pd).then(function (res) {
        if (res.Status === 100) 
        {
           that.setData({info:res.Data});
           that.setData({orderId:res.Data.OrderID});
           var total=Number(res.Data.Price)*Number(res.Data.ItemNum);
           that.setData({total:total.toFixed(2)});
        } 
        else 
        {
          util.showErrorToast('请求失败');
        }
        
      });
    }
    
  }
})